package lsl.leetCode.year2023.test0204_Bisection;

/**
 * 374.猜数字大小
 *
 * @author Zed
 * @description
 * @date 2023/2/4 18:29
 */
//二分法
public class test374 {
    public static void main(String[] args) {
        System.out.println(guessNumber(3));
    }

    public static int guessNumber(int n) {
        int left = 1;
        int right = n;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (guess(mid) == 0) {
                return mid;
            } else if (guess(mid) > 0) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }

    public static int guess(int n) {
        if (2 < n) {
            return -1;
        } else if (2 > n) {
            return 1;
        } else {
            return 0;
        }
    }
}
